import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';

/**
 * 商品 服务
 */
@Injectable()
export class GoodsService extends BaseService {
  step: 0 | 1 | 2 = 0;
  entity: any = { id: 0 };

  reset(){
    this.step = 0;
    this.entity  = { id: 0 };
  }

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/goods';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, classId: any,status:any,allowRetail:any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_title'] = keyword;
    }
    if (classId) {
      page['_filter_eq_classId'] = classId;
    }
    if (allowRetail) {
      page['_filter_eq_allowRetail'] = allowRetail;
    }
    return this.post(url, page);
  }

  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveEntity(entity: any): Promise<any> {
      const url = this.base_url;
      return this.post(url, entity);
  }

  /**
   * 更新商品基础信息
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateEntity(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.put(url, entity);
  }

  /**
   * 更新商品图
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateImgs(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/imgs/' + id;
    return this.put(url, entity);
  }


  /**
   * 更新商品-渠道价格
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updatePrices(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/relates/' + id;
    return this.put(url, entity);
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   * 删除
   *
   * @param ids
   * @returns {Promise<any>}
   */
  deleteEntity(ids): Promise<any> {
    const url = this.base_url + '/';
    return this.delete(url, ids);
  }

  /**
   * 商品上下架
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateStatus(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/status/' + id;
    return this.put(url, entity);
  }


  /**
   *交换排序号
   * @param fromId
   * @param toId
   * @returns {Promise<any>}
   */
  exchangeSortNum(fromId: any, toId: any): Promise<any> {
    console.log(fromId +'==='+toId);
    const url = this.base_url + '/actions/exchange/sortNum/' + fromId + '/' + toId;
    return this.put(url, {});
  }

  /**
   *设置置顶序号
   * @param newId
   * @param entity
   * @returns {Promise<any>}
   */
  setTopNum(newId: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/topNum/' + newId;
    return this.put(url, entity);
  }

  /**
   * 交换置顶排序号
   * @param fromId
   * @param toId
   * @returns {Promise<any>}
   */
  exchangeTopNum(fromId: any, toId: any): Promise<any> {
    const url = this.base_url + '/actions/exchange/topNum/' + fromId + '/' + toId;
    return this.put(url, {});
  }
}
